package com.sparrow.manage.util;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.Properties;

public class SystemUtils {

    /**
     * 获取本机IP地址
     * @return
     * @throws UnknownHostException
     */
    public static String ipAddress() {
        InetAddress ia = null;
        try {
            ia = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return "Unknown";
        }
        return ia.getHostAddress();
    }

    /**
     * 获取操作系统信息
     * @return
     */
    public static String operationSystem(){
        Properties prop = System.getProperties();
        return prop.getProperty("os.name") + "_" + prop.getProperty("os.version");
    }

    /**
     * 获取java信息
     * @return
     */
    public static String javaInfo(){
        Properties prop = System.getProperties();
        return prop.getProperty("java.vm.name") + " JDK" + prop.getProperty("java.version");
    }

    public static DiskSpaceInfo getDiskSpaceInfo(){
        File root = new File("/");

        DiskSpaceInfo diskSpaceInfo = new DiskSpaceInfo();
        diskSpaceInfo.setFreeSpace(root.getFreeSpace());
        diskSpaceInfo.setUsableSpace(root.getUsableSpace());
        diskSpaceInfo.setTotalSpace(root.getTotalSpace());
        diskSpaceInfo.setUsedSpace(root.getTotalSpace() - root.getFreeSpace());

        return diskSpaceInfo;
    }

    public static class DiskSpaceInfo{
        private long freeSpace;   // 空闲空间
        private long usableSpace; // 可用空间
        private long totalSpace;  // 总空间
        private long usedSpace;   // 已用空间

        public long getFreeSpace() {
            return freeSpace;
        }

        public String getFreeSpaceDisplay(){
            return readableFileSize(freeSpace);
        }

        public void setFreeSpace(long freeSpace) {
            this.freeSpace = freeSpace;
        }

        public long getUsableSpace() {
            return usableSpace;
        }

        public String getUsableSpaceDisplay() {
            return readableFileSize(usableSpace);
        }

        public void setUsableSpace(long usableSpace) {
            this.usableSpace = usableSpace;
        }

        public long getTotalSpace() {
            return totalSpace;
        }

        public String getTotalSpaceDisplay(){
            return readableFileSize(totalSpace);
        }

        public void setTotalSpace(long totalSpace) {
            this.totalSpace = totalSpace;
        }

        public long getUsedSpace() {
            return usedSpace;
        }

        public String getUsedSpaceDisplay(){
            return readableFileSize(usedSpace);
        }

        public void setUsedSpace(long usedSpace) {
            this.usedSpace = usedSpace;
        }

        private static final String[] units = new String[]{"B", "kB", "MB", "GB", "TB"};

        private static int getDigitGroups(long size){
            if(size <= 0) return 0;
            return (int) (Math.log10(size) / Math.log10(1024));
        }

        /**
         * 格式化文件大小
         * 参考：https://stackoverflow.com/a/5599842/1253611
         *
         * @param size byte
         * @return readable file size
         */
        public static String readableFileSize(long size) {
            if (size <= 0) return "0";
            int digitGroups = getDigitGroups(size);
            return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
        }
    }

}
